package com.graduation.smartparkingplatform.Controller;

import com.github.pagehelper.PageInfo;
import com.graduation.smartparkingplatform.Pojo.*;
import com.graduation.smartparkingplatform.Response.Response;
import com.graduation.smartparkingplatform.Response.ResponseUtils;
import com.graduation.smartparkingplatform.Service.SpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/space")
public class SpaceController {

  @Autowired SpaceService spaceService;

  // 获取停车位列表
  @GetMapping("/spaceLists")
  public Response getSpaceLists() {
    try {
      List<SpaceAll> spaceLists = spaceService.getSpaceLists();
      return ResponseUtils.buildSuccess(spaceLists);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 获取停车位列表(分页)
  @GetMapping("/spaceList")
  public Response getSpaceList(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count) {
    try {
      PageInfo<Space> spaceList = spaceService.getSpaceList(page, count);
      return ResponseUtils.buildSuccess(spaceList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  //   获取公共区域停车位
  @GetMapping("/spacePublicLists")
  public Response getSpacePublicLists(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count) {
    try {
      PageInfo<SpaceAll> spacePublicLists = spaceService.getSpacePublicLists(page, count);
      return ResponseUtils.buildSuccess(spacePublicLists);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 获取该车位下的私有者信息
  @GetMapping("/privateInfo")
  public Response getPrivateInfo(@RequestParam("spaceId") Integer spaceId) {
    try {
      Private privateInfo = spaceService.getPrivateInfo(spaceId);
      return ResponseUtils.buildSuccess(privateInfo);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 模糊搜索
  @GetMapping("/searchSpace")
  public Response searchSpace(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count,
      @RequestParam(value = "spaceNo", defaultValue = "null") String spaceNo,
      @RequestParam(value = "carPlate", defaultValue = "null") String carPlate) {
    try {
      PageInfo<Space> searchList = spaceService.searchSpace(page, count, spaceNo, carPlate);
      return ResponseUtils.buildSuccess(searchList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 增加停车位
  @PostMapping("/addSpace")
  public Response addSpace(@RequestBody SpaceNA spaceNA) {
    Boolean addSpace = spaceService.addSpace(spaceNA);
    if (addSpace) {
      return ResponseUtils.buildSuccess("增加停车位成功");
    }
    return ResponseUtils.buildError("增加停车位失败");
  }

  // 修改停车位信息
  @PostMapping("/changeInfo")
  public Response changeInfo(@RequestBody SpaceNA spaceNA) {
    Boolean changeInfo = spaceService.changeInfo(spaceNA);
    if (changeInfo) {
      return ResponseUtils.buildSuccess("修改停车位信息成功");
    }
    return ResponseUtils.buildError("修改停车位信息失败");
  }

  // 修改停车位锁定状态
  @PostMapping("/changeStatus")
  public Response changeCompanyStatus(
      @RequestParam("spaceId") Integer spaceId, @RequestParam("status") Boolean status) {
    Boolean changeStatus = spaceService.changeSpaceStatus(spaceId, status);
    if (changeStatus) {
      return ResponseUtils.buildSuccess("修改停车位状态成功");
    }
    return ResponseUtils.buildError("修改停车位状态失败");
  }

  // 删除停车位
  @PostMapping("/deleteSpace")
  public Response deleteSpace(@RequestParam("spaceId") Integer spaceId) {
    Boolean delete = spaceService.deleteSpace(spaceId);
    if (delete) {
      return ResponseUtils.buildSuccess("删除停车位成功");
    }
    return ResponseUtils.buildError("删除停车位失败");
  }

  //  添加停车车辆、修改、删除
  @PostMapping("/addCar")
  public Response addCar(
      @RequestParam("spaceId") Integer spaceId, @RequestParam("carPlate") String carPlate) {
    Boolean addCar = spaceService.addCar(spaceId, carPlate);
    if (addCar) {
      return ResponseUtils.buildSuccess("停车车辆修改成功");
    }
    return ResponseUtils.buildError("停车车辆修改失败");
  }

  // 获取停车位不属于私有车位列表(分页)
  @GetMapping("/noPrivateSpace")
  public Response noPrivateSpace(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count) {
    try {
      PageInfo<Space> spaceList = spaceService.noPrivateSpace(page, count);
      return ResponseUtils.buildSuccess(spaceList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 模糊搜索不属于私有车位列表
  @GetMapping("/searchNoPrivateSpace")
  public Response searchNoPrivateSpace(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count,
      @RequestParam(value = "spaceNo", defaultValue = "null") String spaceNo) {
    try {
      PageInfo<Space> searchList = spaceService.searchNoPrivateSpace(page, count, spaceNo);
      return ResponseUtils.buildSuccess(searchList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 获取收费规则G
  @GetMapping("/getCharge")
  public Response getCharge(@RequestParam(value = "spaceId") Integer spaceId) {
    try {
      Charge chargeInfo = spaceService.getCharge(spaceId);
      return ResponseUtils.buildSuccess(chargeInfo);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }
  // 绑定计费规则
  @PostMapping("/bindCharge")
  public Response bindCharge(
      @RequestParam(value = "spaceId") Integer spaceId,
      @RequestParam(value = "chargeId") Integer chargeId) {
    try {
      Boolean bindCharge = spaceService.bindCharge(spaceId, chargeId);
      if (bindCharge) {
        return ResponseUtils.buildSuccess("绑定计费规则成功");
      }
      return ResponseUtils.buildError("绑定计费规则失败");
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }
}
